package com.clz.hyperit.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.clz.hyperit.dao.AnswerDao;
import com.clz.hyperit.dao.ArticleDao;
import com.clz.hyperit.dao.NoteDao;
import com.clz.hyperit.dao.PositionDao;
import com.clz.hyperit.dao.QuestionDao;
import com.clz.hyperit.dao.UserDao;
import com.clz.hyperit.dao.UserDetailDao;
import com.clz.hyperit.entity.Position;
import com.clz.hyperit.entity.User;
import com.clz.hyperit.entity.UserDetail;
import com.clz.hyperit.exception.ApplicationRunTimeException;
import com.clz.hyperit.service.UserDetailService;
import com.clz.hyperit.util.LogarithmUtil;
import com.clz.hyperit.vo.PageInfo;
import com.clz.hyperit.vo.UserDetailVO;

@Service("userDetailService")
public class UserDetailServiceImpl implements UserDetailService {
	private static Logger logger = LoggerFactory
			.getLogger(UserDetailServiceImpl.class);
	@Autowired
	private UserDetailDao userDetailDao;

	@Autowired
	private UserDao userDao;

	@Autowired
	private ArticleDao articleDao;

	@Autowired
	private NoteDao noteDao;

	@Autowired
	private QuestionDao questionDao;

	@Autowired
	private AnswerDao answerDao;
	
	@Autowired
	private PositionDao positionDao;

	private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

	public UserDetail getUserInfo(Integer userDetail_id) {
		
		UserDetail ud = userDetailDao.get(userDetail_id);
		User user = userDao.get(ud.getUser_id());
		if(user.getStatus()==0){
			throw new  ApplicationRunTimeException("用户被封停");
		}
		else {
			return ud;
		}
	}

	public void updateUserDetail(UserDetail ud) {
		userDetailDao.updateWithNotNullProperties(ud);
	}

	public Boolean isSign(Integer userdetail_id) {
		Date date = new Date();
		String date_1 = df.format(date);
		UserDetail ud = userDetailDao.get(userdetail_id);
		Date date_2 = null;
		try {
			date_2 = df.parse(ud.getSigntime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (date.after(date_2)) {
			ud.setTotal_sign(ud.getTotal_sign() + 1);
			if (daysOfTwo(date_2, date) == 1) {
				ud.setContinous_sign(ud.getContinous_sign() + 1);
			} else {
				ud.setContinous_sign(1);
			}
			ud.setSigntime(date_1);
			ud.setExp(ud.getExp() + 5);
			ud.setHb(ud.getHb() + 1);
			userDetailDao.updateWithNotNullProperties(ud);
			return true;
		} else
			return false;
	}

	// 对比2个日期相差天数
	public static int daysOfTwo(Date fDate, Date oDate) {

		Calendar aCalendar = Calendar.getInstance();

		aCalendar.setTime(fDate);

		int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);

		aCalendar.setTime(oDate);

		int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);

		return day2 - day1;

	}

	// 获取所有用户
	public List<UserDetailVO> getAllUserInfo(PageInfo pageInfo) {
//		UserDetailVO ud_vo = new UserDetailVO(ud,level,null);
		List<UserDetail> ud_list =  userDetailDao.findAll(pageInfo);
		List<UserDetailVO> list = new ArrayList<UserDetailVO>();
		UserDetailVO ud_vo=null;
		int level;
		for(UserDetail ud:ud_list){
			Position position = positionDao.get(ud.getPosition_id());
			level = (int)LogarithmUtil.log(ud.getExp(),2);
			if(position!=null){
				
			     ud_vo = new UserDetailVO(ud,level,position.getName());
			}
			else{
				  ud_vo = new UserDetailVO(ud,level,null);
			}
			   list.add(ud_vo);
		}
		return list;
	}

	// 删除用户 -级联删除 文章 问题 回答 评论 随笔 关注 文章收藏 随笔收藏
	public void deleteUser(Integer id) {
		UserDetail ud = userDetailDao.get(id);
		User user = userDao.get(ud.getUser_id());
		user.setStatus(0);
		ud.setStatus(0);
		userDao.updateWithNotNullProperties(user);
	}

	public void updateUnfreeze(Integer id) {
		UserDetail ud = userDetailDao.get(id);
		User user = userDao.get(ud.getUser_id());
		if(ud.getStatus()==0||user.getStatus()==0){
			user.setStatus(1);
			ud.setStatus(1);
		}
		userDao.updateWithNotNullProperties(user);
		
	}

	public List<UserDetailVO> findUdByArticle(PageInfo pageInfo) {
		List<UserDetail> ud_list =  userDetailDao.findAllByArticle(pageInfo);
		List<UserDetailVO> list = new ArrayList<UserDetailVO>();
		UserDetailVO ud_vo=null;
		int level;
		for(UserDetail ud:ud_list){
			Position position = positionDao.get(ud.getPosition_id());
			level = (int)LogarithmUtil.log(ud.getExp(),2);
			if(position!=null){
				
			     ud_vo = new UserDetailVO(ud,level,position.getName());
			}
			else{
				  ud_vo = new UserDetailVO(ud,level,null);
			}
			   list.add(ud_vo);
		}
		return list;
	}

	public List<UserDetailVO> findUdByFans(PageInfo pageInfo) {
		List<UserDetail> ud_list =  userDetailDao.findAllByFans(pageInfo);
		List<UserDetailVO> list = new ArrayList<UserDetailVO>();
		UserDetailVO ud_vo=null;
		int level;
		for(UserDetail ud:ud_list){
			Position position = positionDao.get(ud.getPosition_id());
			level = (int)LogarithmUtil.log(ud.getExp(),2);
			if(position!=null){
				
			     ud_vo = new UserDetailVO(ud,level,position.getName());
			}
			else{
				  ud_vo = new UserDetailVO(ud,level,null);
			}
			   list.add(ud_vo);
		}
		return list;
	}

	public List<UserDetailVO> findUdByEXP(PageInfo pageInfo) {
		List<UserDetail> ud_list =  userDetailDao.findAllByExp(pageInfo);
		List<UserDetailVO> list = new ArrayList<UserDetailVO>();
		UserDetailVO ud_vo=null;
		int level;
		for(UserDetail ud:ud_list){
			Position position = positionDao.get(ud.getPosition_id());
			level = (int)LogarithmUtil.log(ud.getExp(),2);
			if(position!=null){
				
			     ud_vo = new UserDetailVO(ud,level,position.getName());
			}
			else{
				  ud_vo = new UserDetailVO(ud,level,null);
			}
			   list.add(ud_vo);
		}
		return list;
	}

	public List<UserDetailVO> findUdByNote(PageInfo pageInfo) {
		List<UserDetail> ud_list =  userDetailDao.findUdByNote(pageInfo);
		List<UserDetailVO> list = new ArrayList<UserDetailVO>();
		UserDetailVO ud_vo=null;
		int level;
		for(UserDetail ud:ud_list){
			Position position = positionDao.get(ud.getPosition_id());
			level = (int)LogarithmUtil.log(ud.getExp(),2);
			if(position!=null){
				
			     ud_vo = new UserDetailVO(ud,level,position.getName());
			}
			else{
				  ud_vo = new UserDetailVO(ud,level,null);
			}
			   list.add(ud_vo);
		}
		return list;
	}

	public List<UserDetailVO> findUdByAccept(PageInfo pageInfo) {
		List<UserDetail> ud_list =  userDetailDao.findUdByAccept(pageInfo);
		List<UserDetailVO> list = new ArrayList<UserDetailVO>();
		UserDetailVO ud_vo=null;
		int level;
		for(UserDetail ud:ud_list){
			Position position = positionDao.get(ud.getPosition_id());
			level = (int)LogarithmUtil.log(ud.getExp(),2);
			if(position!=null){
				
			     ud_vo = new UserDetailVO(ud,level,position.getName());
			}
			else{
				  ud_vo = new UserDetailVO(ud,level,null);
			}
			   list.add(ud_vo);
		}
		return list;
	}

	public List<UserDetailVO> searchBlog(PageInfo pageInfo,String search) {
		List<UserDetail> ud_list =  userDetailDao.searchBlog(pageInfo,search);
		List<UserDetailVO> list = new ArrayList<UserDetailVO>();
		UserDetailVO ud_vo=null;
		int level;
		for(UserDetail ud:ud_list){
			Position position = positionDao.get(ud.getPosition_id());
			level = (int)LogarithmUtil.log(ud.getExp(),2);
			if(position!=null){
				
			     ud_vo = new UserDetailVO(ud,level,position.getName());
			}
			else{
				  ud_vo = new UserDetailVO(ud,level,null);
			}
			   list.add(ud_vo);
		}
		return list;
	}

	public List<UserDetailVO> searchBlogByPosition(PageInfo pageInfo,
			Integer position_id) {
		List<UserDetail> ud_list =  userDetailDao.searchBlogByPosition(pageInfo,position_id);
		List<UserDetailVO> list = new ArrayList<UserDetailVO>();
		UserDetailVO ud_vo=null;
		int level;
		for(UserDetail ud:ud_list){
			Position position = positionDao.get(ud.getPosition_id());
			level = (int)LogarithmUtil.log(ud.getExp(),2);
			if(position!=null){
				
			     ud_vo = new UserDetailVO(ud,level,position.getName());
			}
			else{
				  ud_vo = new UserDetailVO(ud,level,null);
			}
			   list.add(ud_vo);
		}
		return list;
	}
}
